package me.ywd.poker.entity;

import me.ywd.poker.Common;

import java.util.Arrays;

/**
 * Created by AOC3 on 2017/2/23.
 */
public class Hand {
    public Poker[] pokers;

    public boolean paired() {
        if (this.getPokers()[0].getNumber().equals(this.getPokers()[1].getNumber())) return true;
        return false;
    }

    public boolean suited() {
        if (this.getPokers()[0].getColor().equals(this.getPokers()[1].getColor())) return true;
        return false;
    }

    public boolean connected(int distant) {
        int thisDistant = this.getPokers()[0].position() - this.getPokers()[1].position();
        if (thisDistant < 0) thisDistant = thisDistant * -1;
        if (thisDistant > 0 && thisDistant <= distant) return true;
        return false;
    }

    public boolean isFormat(String format) {
        if (format.length() == 1) {
            if (this.pokers[0].getNumber().equals(format)) return true;
        } else if (format.length() == 2) {
            if (this.pokers[0].getNumber().equals(format.substring(0, 1)) && this.pokers[1].getNumber().equals(format.substring(1, 2))) return true;
        } else if (format.substring(2).equals("s")) {
            if (this.pokers[0].getNumber().equals(format.substring(0, 1)) && this.pokers[1].getNumber().equals(format.substring(1, 2))) {
                if (this.suited()) return true;
            }
        } else if (format.substring(2).equals("o")) {
            if (this.pokers[0].getNumber().equals(format.substring(0, 1)) && this.pokers[1].getNumber().equals(format.substring(1, 2))) {
                if (!this.suited()) return true;
            }
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        Hand hand = (Hand) o;
        if (this.getPokers()[0].equals(hand.getPokers()[0]) && this.getPokers()[1].equals(hand.getPokers()[1])) {
            return true;
        } else if (this.getPokers()[0].equals(hand.getPokers()[1]) && this.getPokers()[1].equals(hand.getPokers()[0])) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        return this.pokers[0].toString() + this.pokers[1].toString();
    }

    public Poker[] getPokers() {
        return pokers;
    }

    public void setPokers(Poker[] pokers) {
        this.pokers = pokers;
    }
}
